package com.itheima.controller;

import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    private final AliOssUtil aliOssUtil;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public SetmealController(AliOssUtil aliOssUtil) {
        this.aliOssUtil = aliOssUtil;
    }

    //分页查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}", queryPageBean);
        try {
            return setmealService.findPage(queryPageBean);
        } catch (Exception e) {
            log.error("分页查询失败: {}", e.getMessage(), e);
            throw new RuntimeException("分页查询失败，请稍后重试");
        }
    }

    //新增套餐
    @PostMapping("/add")
    public Result addSetmeal(@RequestBody Setmeal setmeal, @RequestParam(value = "checkgroupIds")Integer[] checkgroupIds){
        log.info("新增套餐:{}", setmeal);
        try {
            setmealService.addSetmeal(setmeal, checkgroupIds);
            return new Result(true, "新增成功");
        } catch (Exception e) {
            log.error("新增套餐失败: {}", e.getMessage(), e);
            throw new RuntimeException("新增套餐失败，请稍后重试");
        }
    }

    //阿里云oss添加图片
    @PostMapping("/upload")
    public Result upload(@RequestParam(value = "imgFile") MultipartFile imgFile) {
        if (imgFile == null){
            return new Result(false, "请选择图片");
        }
        log.info("上传图片:{}", imgFile.getOriginalFilename());
        try {
            // 将 MultipartFile 转换为字节数组
            byte[] bytes = imgFile.getBytes();
            // 生成唯一的对象名称（可以根据需求自定义）
            String objectName = System.currentTimeMillis() + "-" + imgFile.getOriginalFilename();

            // 调用 AliOssUtil 的 upload 方法上传文件
            String url = aliOssUtil.upload(bytes, objectName);

            //存储到redis当中
            log.info("阿里云中的图片名称保存到redis当中:{}",objectName);
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES, objectName);

            return new Result(true, "上传成功", url);
        } catch (IOException e) {
            log.error("文件读取失败: {}", e.getMessage(), e);
            throw new RuntimeException("文件读取失败，请稍后重试");
        } catch (Exception e) {
            log.error("上传图片失败: {}", e.getMessage(), e);
            throw new RuntimeException("上传图片失败，请稍后重试");
        }
    }
   //根据id查询套餐
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询套餐:{}", id);
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, "查询成功", setmeal);
        } catch (Exception e) {
            log.error("根据id查询套餐失败: {}", e.getMessage(), e);
            throw new RuntimeException("根据id查询套餐失败，请稍后重试");
        }
    }

    //修改套餐信息 POST /setmeal/edit
    @PostMapping("/edit")
    public Result editSetmeal(@RequestBody Setmeal setmeal, @RequestParam(value = "checkGroupIds")Integer[] checkgroupIds){
        log.info("修改套餐信息:{}", setmeal);
        try {
            setmealService.editSetmeal(setmeal, checkgroupIds);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            log.error("修改套餐信息失败: {}", e.getMessage(), e);
            throw new RuntimeException("修改套餐信息失败，请稍后重试");
        }
    }

}
